<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  Upload emoticons
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Define variable for easier manipulation
		//-----------------------------------------------------------------

		$uploaded_file = $_FILES['req_file'];

		//-----------------------------------------------------------------
		//  Make sure the upload went smooth
		//-----------------------------------------------------------------

		switch ($uploaded_file['error'])
		{
			case 1:
			case 2: header("location: $PHP_SELF?n=modules/emoticons&i=27"); exit; break;
			case 3: header("location: $PHP_SELF?n=modules/emoticons&i=28"); exit; break;
			case 4: header("location: $PHP_SELF?n=modules/emoticons&i=29"); exit; break;
			default: if ($uploaded_file['size'] == 0) { header("location: $PHP_SELF?n=modules/emoticons&i=29"); exit; } break; 
		}

		//-----------------------------------------------------------------
		//  If avatar is uploaded
		//-----------------------------------------------------------------

		if (is_uploaded_file($uploaded_file['tmp_name']))
		{
			//-----------------------------------------------------------------
			//  Define allowed types of avatar and Find dimmensions
			//-----------------------------------------------------------------

			$allowed_types = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
			list($width, $height, ,) = getimagesize($uploaded_file['tmp_name']);

			//-----------------------------------------------------------------
			//  Do some checks
			//-----------------------------------------------------------------

			if (!in_array($uploaded_file['type'], $allowed_types)) { header("location: $PHP_SELF?n=modules/emoticons&i=30"); exit; }
			if ($width > 20){ header("location: $PHP_SELF?n=modules/emoticons&i=31"); exit; }
			if ($height > 20) { header("location: $PHP_SELF?n=modules/emoticons&i=32"); exit; }
			if ($uploaded_file['size'] > 5024) { header("location: $PHP_SELF?n=modules/emoticons&i=33"); exit; }

			//-----------------------------------------------------------------
			//  Upload avatar
			//-----------------------------------------------------------------

			$temp = @move_uploaded_file($uploaded_file['tmp_name'], images.'/'.emoticons.'/'.$uploaded_file['name']);
			@chmod(images.'/'.emoticons.'/'.$uploaded_file['name'], 0644);

			//-----------------------------------------------------------------
			//  If the upload goes bad redirect with error
			//-----------------------------------------------------------------

			if(!$temp) header("location: $PHP_SELF?n=modules/emoticons&i=28");
		}

		//-----------------------------------------------------------------
		//  If avatar is not uploaded show unknown error
		//-----------------------------------------------------------------

		else header("location: $PHP_SELF?n=modules/emoticons&i=34");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/emoticons&i=66");

	break;

	//-----------------------------------------------------------------
	//  Add emoticons
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Begin Check
		//-----------------------------------------------------------------

		if(!$code){ header("location: $PHP_SELF?n=modules/emoticons&i=70"); exit; }

		//-----------------------------------------------------------------
		//  Write Log + Add the emoticon
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc3']).' <b>emoticons</b> the emoticon code -> \''.$db->escape($code).'\'','date' => ''.time().''));
		$db->insertRow("emoticons",array('code' => ''.$db->escape($code).'','pic' => ''.$db->escape($pic).''));

		//-----------------------------------------------------------------
		//  Add the emoticon
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/emoticons&i=67");

	break;

	//-----------------------------------------------------------------
	//  Delete Emoticons
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Write Log + Delete the emoticon
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT * FROM emoticons WHERE id = '".$db->escape($d)."'");
		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc']).' <b>emoticons</b> the emoticon with code -> \''.$db->escape($checker['code']).'\'','date' => ''.time().''));
		$db->deleteRow("emoticons","id = '".$db->escape($d)."'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/emoticons&i=68");

	break;

	//-----------------------------------------------------------------
	//  Update Emoticons
	//-----------------------------------------------------------------

	case 4:

		//-----------------------------------------------------------------
		//  Begin Check
		//-----------------------------------------------------------------

		foreach($code as $idx=>$val) if(!$code[$idx]){ header("location: $PHP_SELF?n=modules/emoticons&i=70"); exit; }
		foreach($pic as $idx=>$val) if(!$pic[$idx]){ header("location: $PHP_SELF?n=modules/emoticons&i=71"); exit; }

		//-----------------------------------------------------------------
		//  Save the emoticons
		//-----------------------------------------------------------------
	
		foreach($code as $idx=>$val)
			$db->query("Update `emoticons` set `code` = '".$db->escape($val)."' WHERE `id` = '".$db->escape($idx)."'");

		foreach($pic as $idx=>$val)
			$db->query("Update `emoticons` set `pic` = '".$db->escape($val)."' WHERE `id` = '".$db->escape($idx)."'");

		//-----------------------------------------------------------------
		//  Write Log
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc2']).' <b>emoticons</b>','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/emoticons&i=69");

	break;

	//-----------------------------------------------------------------
	//  View Emoticons Form
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Load Emoticons table
		//-----------------------------------------------------------------

		$emoticons_db = $db->getTable("SELECT * FROM emoticons");

		if($emoticons_db != FALSE)
		{
			foreach($emoticons_db as $idx=>$emoticon)
			{
				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$layout .= "
					<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
					<td width='5%' align='center'><img src='images/emoticons/{$emoticon['pic']}' alt='{$emoticon['code']}'></td>
					<td width='8%'><input type='text' name='code[{$emoticon['id']}]' value='{$emoticon['code']}' maxlength='3' class='box' style='width:100%'></td>
					<td width='82%'><input type='text' name='pic[{$emoticon['id']}]' value='{$emoticon['pic']}' maxlength='50' class='box' style='width:100%'></td>
					<td width='5%'><a href='$PHP_SELF?n=modules/emoticons&a=3&d={$emoticon['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Emoticons form desc4']}'></a></td>
					</tr>";
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "
				<tr class='con1'>
				<td colspan='4'>{$language['Emoticons form desc7']}</td>
				</tr>\n";
		}
		
		//-----------------------------------------------------------------
		//  Load dropdown_form for templates directory
		//-----------------------------------------------------------------

		$array_template = array();
		$dir_template = opendir( 'images/emoticons' );

		while ($file_template = readdir($dir_template))
			if(!preg_match( "/^..?$|^index|htm$|Thumbs$|html$|^\./i", $file_template))
				$array_template[] = array($file_template, $file_template);

		closedir($dir_template);
		sort($array_template);
		reset($array_template);

		$default = array();
		$default = $array_template[0] 
		
		?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='con1'><td><a href='<?= $PHP_SELF ?>?n=modules/admin'><?= $language['Admin name'] ?></a> &raquo; <?= $language['Emoticons title'] ?></td></tr>
			</table>
			<table><tr><td></td></tr></table>			
		
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/emoticons" enctype="multipart/form-data">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="4"><?= $language['Emoticons upload title'] ?></td></tr>
			<tr class="bottom"><td width="80%"><?= $language['Emoticons upload desc'] ?></td><td width="20%"><?= $language['Emoticons upload desc2'] ?></td></tr>
			<tr class="con1"><td><input name="req_file" class="box" type="file" size="72"></td><td><input type="submit" name="upload" style="width:100%" class="box" value="<?= $language['Emoticons upload desc3'] ?>"></td></tr>
			<input type="hidden" name="a" value="1">
			</table></form>
			<table><tr><td></td></tr></table>			
			
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/emoticons" name='add_emoticon'>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="4"><?= $language['Emoticons add title'] ?></td></tr>
			<tr class="bottom"><td width="8%"><?= $language['Emoticons add desc'] ?></td><td width="77%"><?= $language['Emoticons add desc2'] ?></td><td width="5%" align="center"><?= $language['Emoticons add desc3'] ?></td><td><?= $language['Emoticons add desc4'] ?></td></tr>
			<tr class="con1"><td><input type='text' name='code' maxlength='3' class='box' style='width:100%'></td><td><?= form_dropdown('pic', $array_template, '$default[1]', "style='width:100%'", "class='box'", "onChange='show_emo(\"images/emoticons\")'") ?></td><td><img src='images/emoticons/<?= $default[1] ?>' name='emoticon_preview' border='0' alt='<?= $default[1] ?>'></td><td><input type='submit' name='submit' class='box' value='<?= $language['Emoticons add desc5'] ?>'></td></tr>
			<input type="hidden" name="a" value="2">
			</table></form>
			<table><tr><td></td></tr></table>			

			<form method="post" action="<?= $PHP_SELF ?>?n=modules/emoticons">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="4"><?= $language['Emoticons form title'] ?></td></tr>
			<tr class="bottom"><td align="center"><?= $language['Emoticons form desc'] ?></td><td><?= $language['Emoticons form desc2'] ?></td><td colspan="2"><?= $language['Emoticons form desc3'] ?></td></tr>
			<?= $layout ?>
			</table>
			<table><tr><td></td></tr></table>			

			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='con1'>
			<td width="50%"><?= $language['Emoticons form desc5'] ?></td>
			<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Emoticons form desc6'] ?>' tabindex='3'></td>
			</tr>
			<input type="hidden" name="a" value="4">
			</table></form>
		<?
}
?>